﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace LanDBom.Models
{
    public class LanguageFilterAttribute: ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // 尝试从会话中获取语言设置  
            string language = filterContext.HttpContext.Session["Language"] as string;

            // 如果会话中没有语言设置，则使用默认语言（比如英语）  
            if (string.IsNullOrEmpty(language))
            {
                language = "en";
            }

            // 根据语言设置创建 CultureInfo 对象  
            CultureInfo cultureInfo = new CultureInfo(language);

            // 设置当前线程的文化信息和 UI 文化信息  
            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;

            // 调用基类方法，继续执行后续操作  
            base.OnActionExecuting(filterContext);
        }
    }
}